package _5thread;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @description:
 * @author: wang zhen
 * @create: 2022-06-09 14:42
 */
public class QueueTest {

    public static void main(String[] args) {

        BlockingDeque<Integer> deque = new LinkedBlockingDeque(2);

        //生产
        Thread consume = new Thread("1号") {

            @Override
            public void run() {

                try {

                    for (int i = 0; i < 4; i++) {
                        System.out.println("准备生产");
                        deque.put(2);
                        System.out.println("生产1个");
                        Thread.sleep(2000);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            }
        };

        //消费1号
        Thread thread1 = new Thread("1号") {

            @Override
            public void run() {
                try {
                    for (; ; ) {
                        Integer poll = deque.take();
                        System.out.println(Thread.currentThread().getName() + "消费1个");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };

        //消费2号
        Thread thread2 = new Thread("2号") {

            @Override
            public void run() {

                try {
                    for (; ; ) {
                        Integer poll = deque.take();
                        System.out.println(Thread.currentThread().getName() + "消费1个");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            }
        };

        consume.start();
        thread1.start();
        thread2.start();
    }
}
